package com.zhongwang.business.module.estimateBudgetAccountingManage.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhongwang.business.base.BaseService;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.*;
import com.zhongwang.business.module.contractManage.model.ConContractMainModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.GetDataDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.*;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelExport.CostSheet;
import com.zhongwang.business.util.BeanUtil;
import com.zhongwang.business.util.FileUtils;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * AccountingService 核算业务逻辑层
 *
 * @author 徐一贺
 * @version 1.0
 * @date 2020/9/15 8:23
 */
@Log4j2
@Service
public class AccountingService extends BaseService {
    @Autowired
    private GetDataService getDataService;

    @Autowired
    private AccountingDao accountingDao;

    @Autowired
    private GetDataDao getDataDao;

    @Autowired
    private AccountingInfoDao accountingInfoDao;
    @Value("${user.upload.path}")
    private String uploadPath;

    /**
     * list 根据查询条件检索待核算查询列表
     *
     * @param param 查询条件
     * @return AccountingProjectModel 查询结果
     * @author 徐一贺
     * @date 2020/9/15 9:43
     * @version 1.0
     */
    public List<AccountingProjectModel> list(AccountingProjectModel param) {
        param.setCurrentUserId(ShiroUtils.getUserId());
        return accountingDao.findAccountingProjectListByCriteria(param);
    }

    /**
     * queryList 根据查询条件检索核算查询列表
     *
     * @param param 查询条件
     * @return AccountingProjectModel 查询结果
     * @author 徐一贺
     * @date 2020/9/15 9:43
     * @version 1.0
     */
    public List<AccountingProjectModel> queryList(AccountingProjectModel param) {
        param.setCurrentUserId(ShiroUtils.getUserId());
        return accountingDao.findAccountingProjectListByCriteriaFroQueryList(param);
    }

    /**
     * findBusBudgetAccountingBasicInfoConfigList 依据单据表主键获取参数列表
     *
     * @param batId 单据表主键
     * @return
     * @author 徐一贺
     * @date 2020/10/7 10:17
     * @version 1.0
     */
    public List<BusBudgetAccountingBasicInfoConfig> findBusBudgetAccountingBasicInfoConfigList(
            Integer batId) {
        return accountingDao.findBusBudgetAccountingBasicInfoConfigList(batId);
    }

    /**
     * saveParameters 保存参数
     *
     * @param
     * @return
     * @author 徐一贺
     * @date 2020/10/7 12:37
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult saveParameters(JSONArray params, Integer batId) {
        JsonResult jsonResult = new JsonResult();
        try {
            for (int i = 0; i < params.size(); i++) {
                JSONObject myjObject = params.getJSONObject(i);
                Map objMap = myjObject;
                BusBudgetAccountingBasicInfoConfig busBudgetAccountingBasicInfoConfig =
                        new BusBudgetAccountingBasicInfoConfig();
                busBudgetAccountingBasicInfoConfig.setId(Integer.valueOf(objMap.get("id").toString()));
                busBudgetAccountingBasicInfoConfig.setValue(objMap.get("value").toString());
                accountingDao.updateBusBudgetAccountingBasicInfoConfigValueByPrimaryKey(
                        busBudgetAccountingBasicInfoConfig);
                BusBudgetAccountingBasicInfoConfig configBasicInfo = accountingDao.getBusBudgetAccountingBasicInfoConfigByPrimaryKey(Integer.valueOf(objMap.get("id").toString()));
                BusMachiningEquipmentQuotaVersion version = accountingDao.getMacQuotaConfigVersionByCode(configBasicInfo.getValue());
                if (version != null && "Machining quota".equals(configBasicInfo.getCode())) {
                    BusMachiningHours macCondition = new BusMachiningHours();
                    macCondition.setBmeVersion(version.getVersion());
                    macCondition.setBatId(batId);
                    accountingDao.updateBusMachiningHoursByCondition(macCondition);
                }
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setData(findBusBudgetAccountingBasicInfoConfigList(batId));
            jsonResult.setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败！");
        }
        return jsonResult;
    }

    /**
     * saveItemTexture 保存材质
     *
     * @param params 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/8 13:41
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult saveItemTexture(BusBudgetAccountingCost params) {
        JsonResult jsonResult = new JsonResult();
        try {
            accountingDao.updateBusBudgetAccountingCostTextureByPrimaryKey(params);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("保存成功！");
            jsonResult.setData(params.getTexture());
            log.info("保存材质成功！：");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败！");
            log.error("保存材质失败：" + e.getMessage());
        }
        return jsonResult;
    }

    /**
     * startAccounting 一键核算 需求 将各个子项的数据进行运算把运算结果交给父级
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/26 18:40
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult startAccounting(BusBudgetAccountingTaskModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            //            String[] showOrderArray = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
            //                    "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
            //                    "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十",
            //                    "三十一", "三十二", "三十三", "三十四", "三十五", "三十六", "三十七", "三十八", "三十九", "四十",
            //                    "四十一", "四十二", "四十三", "四十四", "四十五", "四十六", "四十七", "四十八", "四十九", "五十",
            //                    "五十一", "五十二", "五十三", "五十四", "五十五", "五十六", "五十七", "五十八", "五十九", "六十",
            //                    "六十一", "六十二", "六十三", "六十四", "六十五", "六十六", "六十七", "六十八", "六十九", "七十",
            //                    "七十一", "七十二", "七十三", "七十四", "七十五", "七十六", "七十七", "七十八", "七十九", "八十",
            //                    "八十一", "八十二", "八十三", "八十四", "八十五", "八十六", "八十七", "八十八", "八十九", "九十",
            //                    "九十一", "九十二", "九十三", "九十四", "九十五", "九十六", "九十七", "九十八", "九十九", "一百",
            //                    "一百零一", "一百零二", "一百零三", "一百零四", "一百零五", "一百零六", "一百零七", "一百零八", "一百零九",
            // "一百一十"};
            BusBudgetAccountingTask order =
                    getDataDao.getBusBudgetAccountingTaskByPrimaryKey(param.getId());
            // 校验计算参数如果计算参数有没有填写的情况则报必须填写
            List<BusBudgetAccountingBasicInfoConfig> projectAccountingBasicInfoConfigList =
                    getDataDao.findBusBudgetAccountingBasicInfoConfigListByBatId(param.getId());
            for (int i = 0; i < projectAccountingBasicInfoConfigList.size(); i++) {
                if (projectAccountingBasicInfoConfigList.get(i).getValue() == null
                        || projectAccountingBasicInfoConfigList.get(i).getValue().trim().equals("")) {
                    jsonResult.setType(JsonResult.ResultType.ERROR);
                    jsonResult.setMsg("尊敬的用户，您计算所用基础参数不能为空，请您补充！");
                    return jsonResult;
                }
            }
            // 获取成本项列表
            List<BusBudgetAccountingCostModel> costList =
                    getDataDao.findBusBudgetAccountingCostByBatId(order.getId(), 5);
            // 获取项目信息
            VwProject project = getDataDao.getVwProjectByCode(order.getProCode());
            // 获取废料回收系数
            BusBudgetAccountingBasicInfoConfigModel paramCondition =
                    new BusBudgetAccountingBasicInfoConfigModel();
            paramCondition.setBatId(order.getId());
            paramCondition.setCode("Coefficient of waste recovery");
            // 获取废料回收系数
            BigDecimal coefficientWasteRecovery =
                    getDataDao.getBusBudgetAccountingBasicInfoConfigByCondition(paramCondition);
            paramCondition.setCode("Carbon steel recovery");
            // 获取废料碳钢回收单价
            BigDecimal carbonSteelRecovery =
                    getDataDao.getBusBudgetAccountingBasicInfoConfigByCondition(paramCondition);
            paramCondition.setCode("Stainless steel recycling");
            // 获取废料不锈钢钢回收单价
            BigDecimal stainlessSteelRecycling =
                    getDataDao.getBusBudgetAccountingBasicInfoConfigByCondition(paramCondition);
            // 计算 所有 碳钢 不锈钢 铝材 对应项的废料回收费 回写对应材料采购费的备注
            for (BusBudgetAccountingCostModel cost : costList) {
                // 设置被除数
                BigDecimal divisor = new BigDecimal(0);
                switch (cost.getAmountDisplayModel().intValue()) {
                    case 1:
                        divisor = new BigDecimal(1);
                        break;
                    case 2:
                        divisor = new BigDecimal(10000);
                        break;
                    case 3:
                        divisor = new BigDecimal(1000000);
                        break;
                    default:
                        divisor = new BigDecimal(100000000);
                        break;
                }
                cost.setVerId(5);
                if (cost.getCode().contains("-材料费-材料采购费-碳钢")
                        || cost.getCode().contains("-材料费-材料采购费-不锈钢")
                        || cost.getCode().contains("-材料费-材料采购费-铝材")) {
                    // 获取碳钢总料重
                    BusBudgetAccountingEstimatesData carbonSteelTotalWeightCondition =
                            new BusBudgetAccountingEstimatesData();
                    carbonSteelTotalWeightCondition.setBatId(cost.getBatId());
                    carbonSteelTotalWeightCondition.setProCode(order.getProCode());
                    carbonSteelTotalWeightCondition.setUseType(2);
                    if (cost.getCode().contains("-材料费-材料采购费-碳钢")) {
                        carbonSteelTotalWeightCondition.setDataType("该项目材料费碳钢总料重");
                    } else if (cost.getCode().contains("-材料费-材料采购费-不锈钢")) {
                        carbonSteelTotalWeightCondition.setDataType("该项目材料费不锈钢总料重");
                    } else if (cost.getCode().contains("-材料费-材料采购费-铝材")) {
                        carbonSteelTotalWeightCondition.setDataType("该项目材料费铝材总料重");
                    }
                    carbonSteelTotalWeightCondition.setDataMajor(cost.getMajor());
                    carbonSteelTotalWeightCondition.setDataPart(cost.getPart());
                    carbonSteelTotalWeightCondition.setCostId(cost.getId());
                    BusBudgetAccountingEstimatesData totalWeightObj =
                            getDataDao.getBusBudgetAccountingEstimatesDataByCondition(
                                    carbonSteelTotalWeightCondition);
                    // 获取总料重
                    String totalWeightStr = totalWeightObj == null ? "0.00" : totalWeightObj.getDataValue();
                    BigDecimal totalWeight =
                            new BigDecimal(totalWeightStr)
                                    .divide(new BigDecimal(project.getNum()), 8, BigDecimal.ROUND_HALF_UP);
                    // 获取总净重
                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new BusGeneralHoursModel();
                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
                    carbonSteelTotalNetWeightCondition.setPart(cost.getPart());
                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
                    BigDecimal recoveryPrice = new BigDecimal(0);
                    if (cost.getCode().contains("-碳钢")) {
                        recoveryPrice = carbonSteelRecovery;
                    } else if (cost.getCode().contains("-不锈钢")) {
                        recoveryPrice = stainlessSteelRecycling;
                    }
                    if (cost.getMajor().equals("机械")) {
                        carbonSteelTotalNetWeightCondition.setTypes("1");
                    } else if (cost.getMajor().equals("流体")) {
                        carbonSteelTotalNetWeightCondition.setTypes("2");
                    } else if (cost.getMajor().equals("自动化")) {
                        carbonSteelTotalNetWeightCondition.setTypes("3");
                    }
                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
                    if (cost.getCode().contains("-碳钢")) {
                        carbonSteelTotalNetWeightCondition.setLaborName("-碳钢");
                    } else if (cost.getCode().contains("-不锈钢")) {
                        carbonSteelTotalNetWeightCondition.setLaborName("-不锈钢");
                    } else if (cost.getCode().contains("-铝材")) {
                        carbonSteelTotalNetWeightCondition.setLaborName("-铝材");
                    }
                    BigDecimal totalNetWeight =
                            getDataDao
                                    .getTotalNumByCondition(carbonSteelTotalNetWeightCondition)
                                    .divide(new BigDecimal(project.getNum()), 8, BigDecimal.ROUND_HALF_UP);

                    if (totalNetWeight.compareTo(new BigDecimal(0)) == 1
                            && totalWeight.compareTo(new BigDecimal(0)) == 1) {
                        cost.setRemark(
                                "净重："
                                        + Double.valueOf(
                                        totalNetWeight.setScale(2, BigDecimal.ROUND_HALF_UP).toString())
                                        + "吨，损耗："
                                        + Double.valueOf(
                                        totalWeight
                                                .subtract(totalNetWeight)
                                                .divide(totalWeight, 8, BigDecimal.ROUND_HALF_UP)
                                                .multiply(new BigDecimal(100))
                                                .setScale(2, BigDecimal.ROUND_HALF_UP)
                                                .toString())
                                        + "%");
                    } else if (totalNetWeight.compareTo(new BigDecimal(0)) == 1
                            && totalWeight.compareTo(new BigDecimal(0)) == 0) {
                        cost.setRemark(
                                "净重："
                                        + Double.valueOf(
                                        totalNetWeight.setScale(2, BigDecimal.ROUND_HALF_UP).toString())
                                        + "吨，损耗：0%");
                    } else {
                        cost.setRemark("净重：0吨，损耗：0%");
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                    // 获取对应的废料回收费
                    BusBudgetAccountingCostModel wasteCost = new BusBudgetAccountingCostModel();
                    BeanUtil.copy(cost, wasteCost);
                    wasteCost.setCode(wasteCost.getCode().replace("-材料采购费-", "-废料回收费-"));
                    wasteCost.setVerId(5);
                    wasteCost = accountingDao.getBusBudgetAccountingCostByCondition2(wasteCost);
                    wasteCost.setNum(totalWeight.subtract(totalNetWeight).multiply(coefficientWasteRecovery));
                    wasteCost.setDisplayNum(
                            totalWeight
                                    .subtract(totalNetWeight)
                                    .multiply(coefficientWasteRecovery)
                                    .setScale(2, BigDecimal.ROUND_HALF_UP)
                                    .toString());
                    if (cost.getCode().contains("-碳钢") || cost.getCode().contains("-不锈钢")) {
                        wasteCost.setPrice(recoveryPrice);
                        if (cost.getAmountDisplayModel().intValue() == 1) {
                            wasteCost.setDisplayPrice(
                                    recoveryPrice.setScale(0, BigDecimal.ROUND_HALF_UP).toString());
                        } else {
                            wasteCost.setDisplayPrice(
                                    recoveryPrice.divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
                        }
                    } else {
                        BigDecimal price = cost.getPrice() == null ? new BigDecimal(0.00) : cost.getPrice();
                        wasteCost.setPrice(price);
                        if (wasteCost.getAmountDisplayModel().intValue() == 1) {
                            wasteCost.setDisplayPrice(price.setScale(0, BigDecimal.ROUND_HALF_UP).toString());
                        } else {
                            wasteCost.setDisplayPrice(
                                    price.divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
                        }
                    }
                    wasteCost.setTotalPrice(
                            totalWeight
                                    .subtract(totalNetWeight)
                                    .multiply(coefficientWasteRecovery)
                                    .multiply(wasteCost.getPrice()));
                    if (wasteCost.getAmountDisplayModel().intValue() == 1) {
                        wasteCost.setDisplayTotalPrice(
                                totalWeight
                                        .subtract(totalNetWeight)
                                        .multiply(coefficientWasteRecovery)
                                        .multiply(wasteCost.getPrice())
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        wasteCost.setDisplayTotalPrice(
                                totalWeight
                                        .subtract(totalNetWeight)
                                        .multiply(coefficientWasteRecovery)
                                        .multiply(wasteCost.getPrice())
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(wasteCost);
                } else if (cost.getCode().contains("-材料费-材料采购费-其他")) {
                    cost.setNum(null);
                    cost.setDisplayNum(null);
                    cost.setPrice(null);
                    cost.setDisplayPrice(null);
                    accountingDao.updateMatOtherInfoToClaer(cost);

                    // 获取父级项
                    BusBudgetAccountingCostModel matPurFeeCost =
                            accountingDao.getBusBudgetAccountingCostByBatIdAndParentCode(cost);
                    // 然后把该父级项所以子集项相加的值加在父级项上
                    // 获取总净重
                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new BusGeneralHoursModel();
                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
                    if (cost.getMajor().equals("机械")) {
                        carbonSteelTotalNetWeightCondition.setTypes("1");
                    } else if (cost.getMajor().equals("流体")) {
                        carbonSteelTotalNetWeightCondition.setTypes("2");
                    } else if (cost.getMajor().equals("自动化")) {
                        carbonSteelTotalNetWeightCondition.setTypes("3");
                    }
                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
                    BigDecimal totalNetWeight =
                            getDataDao
                                    .getTotalNumByCondition(carbonSteelTotalNetWeightCondition)
                                    .divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
                    // 获取子集集合去掉其他
                    matPurFeeCost.setVerId(5);
                    BusBudgetAccountingEstimatesData carbonSteelTotalWeightCondition =
                            new BusBudgetAccountingEstimatesData();
                    carbonSteelTotalWeightCondition.setBatId(cost.getBatId());
                    carbonSteelTotalWeightCondition.setProCode(order.getProCode());
                    carbonSteelTotalWeightCondition.setUseType(2);
                    carbonSteelTotalWeightCondition.setDataType("'该项目材料费碳钢总料重','该项目材料费不锈钢总料重','该项目材料费铝材总料重'");
                    carbonSteelTotalWeightCondition.setDataMajor(cost.getMajor());
                    carbonSteelTotalWeightCondition.setDataPart(cost.getPart());
                    // 获取总料重
                    BigDecimal matPurFeeCostTotalWeight =
                            getDataDao
                                    .getMatPurFeeCostTotalWeightByCondition(carbonSteelTotalWeightCondition)
                                    .divide(new BigDecimal(project.getNum()), 8, BigDecimal.ROUND_HALF_UP);
                    // 获取总净重
                    matPurFeeCost.setNum(accountingDao.getTotalNumByBatIdAndParentCode(matPurFeeCost));
                    matPurFeeCost.setDisplayNum(
                            accountingDao
                                    .getTotalNumByBatIdAndParentCode(matPurFeeCost)
                                    .setScale(2, BigDecimal.ROUND_HALF_UP)
                                    .toString());

                    matPurFeeCost.setRemark("");

                    matPurFeeCost.setTotalPrice(
                            accountingDao.getTotalPriceByBatIdAndParentCode(matPurFeeCost));
                    if (matPurFeeCost.getAmountDisplayModel() == 1) {
                        matPurFeeCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(matPurFeeCost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        matPurFeeCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(matPurFeeCost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(matPurFeeCost);
                }
                // 回写废料回收费 和 材料费
                else if (cost.getCode().contains("-材料费-废料回收费-其他")) {
                    // 获取父级项
                    BusBudgetAccountingCostModel WasteRecyclingFeeCost =
                            accountingDao.getBusBudgetAccountingCostByBatIdAndParentCode(cost);
                    WasteRecyclingFeeCost.setNum(
                            accountingDao.getTotalNumByBatIdAndParentCode(WasteRecyclingFeeCost));
                    WasteRecyclingFeeCost.setDisplayNum(
                            accountingDao
                                    .getTotalNumByBatIdAndParentCode(WasteRecyclingFeeCost)
                                    .setScale(2, BigDecimal.ROUND_HALF_UP)
                                    .toString());
                    // 然后把该父级项所以子集项相加的值加在父级项上
                    WasteRecyclingFeeCost.setTotalPrice(
                            accountingDao.getTotalPriceByBatIdAndParentCode(WasteRecyclingFeeCost));
                    if (WasteRecyclingFeeCost.getAmountDisplayModel().intValue() == 1) {
                        WasteRecyclingFeeCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(WasteRecyclingFeeCost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        WasteRecyclingFeeCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(WasteRecyclingFeeCost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    WasteRecyclingFeeCost.setRemark(
                            "废料总重："
                                    + Double.valueOf(
                                    accountingDao
                                            .getTotalNumByBatIdAndParentCode(WasteRecyclingFeeCost)
                                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                                            .toString())
                                    + "吨");
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(WasteRecyclingFeeCost);
                    // 获取父级项 材料费
                    BusBudgetAccountingCost matFeeCost =
                            accountingDao.getBusBudgetAccountingCostByBatIdAndParentCode(WasteRecyclingFeeCost);
                    // 获取材料采购费
                    BusBudgetAccountingCostModel matPurFeeCondition = new BusBudgetAccountingCostModel();
                    matPurFeeCondition.setBatId(matFeeCost.getBatId());
                    matPurFeeCondition.setMajor(matFeeCost.getMajor());
                    matPurFeeCondition.setPart(matFeeCost.getPart());
                    matPurFeeCondition.setParentCode(matFeeCost.getCode());
                    matPurFeeCondition.setCode("-材料费-材料采购费");
                    matPurFeeCondition.setVerId(5);
                    BusBudgetAccountingCostModel matPurFeeCost =
                            accountingDao.getBusBudgetAccountingCostByParentCodeAndCondition(matPurFeeCondition);
                    if (matPurFeeCost.getTotalPrice() != null) {
                        matFeeCost.setTotalPrice(
                                matPurFeeCost.getTotalPrice().subtract(WasteRecyclingFeeCost.getTotalPrice()));
                        if (matFeeCost.getAmountDisplayModel().intValue() == 1) {
                            matFeeCost.setDisplayTotalPrice(
                                    matPurFeeCost
                                            .getTotalPrice()
                                            .subtract(WasteRecyclingFeeCost.getTotalPrice())
                                            .setScale(0, BigDecimal.ROUND_HALF_UP)
                                            .toString());
                        } else {
                            matFeeCost.setDisplayTotalPrice(
                                    matPurFeeCost
                                            .getTotalPrice()
                                            .subtract(WasteRecyclingFeeCost.getTotalPrice())
                                            .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                            .toString());
                        }
                    } else {
                        matFeeCost.setTotalPrice(new BigDecimal(0));
                        matFeeCost.setDisplayTotalPrice("0");
                    }

                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(matFeeCost);
                }
                // 回写制造费
                else if (cost.getCode().contains("-制造费")
                        && cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-制造费")) {
                    cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    // 获取总净重
                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new BusGeneralHoursModel();
                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
                    carbonSteelTotalNetWeightCondition.setTypes("1,2,3");
                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
                    BigDecimal totalNetWeight =
                            getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
                    if (totalNetWeight.compareTo(new BigDecimal(0)) == 1) {
                        cost.setManPrice(
                                cost.getTotalPrice().divide(totalNetWeight.divide(new BigDecimal(project.getNum()), 8, BigDecimal.ROUND_HALF_UP), 8, BigDecimal.ROUND_HALF_UP));
                        if (cost.getAmountDisplayModel().intValue() == 1) {
                            cost.setRemark(
                                    "制造单价：" + cost.getManPrice().setScale(0, BigDecimal.ROUND_HALF_UP) + "元/吨");
                        } else {
                            cost.setRemark(
                                    "制造单价："
                                            + cost.getManPrice().divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                            + "万元/吨");
                        }
                    } else {
                        if (cost.getAmountDisplayModel().intValue() == 1) {
                            cost.setRemark("");
                        } else {
                            cost.setRemark("");
                        }
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
                // 回写外协费
                else if (cost.getCode().contains("-外协费")
                        && cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-外协费")) {
                    cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
                // 回写人工费 现场安装费
                else if (cost.getCode().contains("-现场安装费")
                        && cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-现场安装费")) {
                    BusBudgetAccountingCostModel artificialCostCondition = new BusBudgetAccountingCostModel();
                    artificialCostCondition.setBatId(cost.getBatId());
                    artificialCostCondition.setMajor(cost.getMajor());
                    artificialCostCondition.setPart(cost.getPart());
                    artificialCostCondition.setParentCode(cost.getCode());
                    artificialCostCondition.setCode("-现场安装费-人工");
                    artificialCostCondition.setVerId(5);
                    // 获取人工成本项
                    BusBudgetAccountingCostModel artificialCost =
                            accountingDao.getBusBudgetAccountingCostByParentCodeAndCondition(
                                    artificialCostCondition);
                    artificialCost.setTotalPrice(
                            accountingDao.getTotalPriceByBatIdAndParentCode(artificialCost));
                    if (artificialCost.getAmountDisplayModel().intValue() == 1) {
                        artificialCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(artificialCost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        artificialCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(artificialCost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(artificialCost);
                    // 回写现场安装费
                    // 获取总净重
                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new BusGeneralHoursModel();
                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
                    carbonSteelTotalNetWeightCondition.setPart(cost.getPart());
                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
                    carbonSteelTotalNetWeightCondition.setTypes("1,2,3");
                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
                    BigDecimal totalNetWeight =
                            getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
                    cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    if (totalNetWeight.compareTo(new BigDecimal(0)) == 1) {
                        cost.setInstallPrice(
                                cost.getTotalPrice().divide(totalNetWeight.divide(new BigDecimal(project.getNum()), 8, BigDecimal.ROUND_HALF_UP), 8, BigDecimal.ROUND_HALF_UP));
                        if (cost.getAmountDisplayModel().intValue() == 1) {
                            cost.setRemark(
                                    "安装单价：" + cost.getInstallPrice().setScale(0, BigDecimal.ROUND_HALF_UP) + "元/吨");
                        } else {
                            cost.setRemark(
                                    "安装单价："
                                            + cost.getInstallPrice().divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                            + "万元/吨");
                        }
                    } else {
                        if (cost.getAmountDisplayModel().intValue() == 1) {
                            cost.setRemark("");
                        } else {
                            cost.setRemark("");
                        }
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
//                else if (cost.getCode().contains("-现场安装费-辅材")
//                        && cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-辅材")) {
//                    cost.setRemark("安装人工");
//                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
//                }
                // 回写部分
                else if (cost.getCode().contains("-设计费")
                        && cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-设计费")) {
                    BusBudgetAccountingCostModel partCostCondition = new BusBudgetAccountingCostModel();
                    partCostCondition.setBatId(cost.getBatId());
                    partCostCondition.setMajor(cost.getMajor());
                    partCostCondition.setPart(cost.getPart());
                    partCostCondition.setCode(cost.getParentCode());
                    partCostCondition.setVerId(5);
                    // 获取部分项
                    BusBudgetAccountingCostModel partCost =
                            accountingDao.getBusBudgetAccountingCostByParentCodeAndCondition(partCostCondition);
                    partCost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(partCost));
                    if (partCost.getAmountDisplayModel().intValue() == 1) {
                        partCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(partCost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        partCost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(partCost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(partCost);
                }
                // 回写专业和小计
                else if (cost.getCode().equals("小计")) {
                    List<BusBudgetAccountingCostModel> majorCostList =
                            accountingDao.findBusBudgetAccountingCostByParentCodeAndBatId(cost);
                    for (BusBudgetAccountingCostModel majorCost : majorCostList) {
                        majorCost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(majorCost));
                        if (majorCost.getAmountDisplayModel().intValue() == 1) {
                            majorCost.setDisplayTotalPrice(
                                    accountingDao
                                            .getTotalPriceByBatIdAndParentCode(majorCost)
                                            .setScale(0, BigDecimal.ROUND_HALF_UP)
                                            .toString());
                        } else {
                            majorCost.setDisplayTotalPrice(
                                    accountingDao
                                            .getTotalPriceByBatIdAndParentCode(majorCost)
                                            .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                            .toString());
                        }
                        accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(majorCost);
                    }
                    cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
                // 回写包装费
                else if (cost.getCode().equals("包装运输费")) {
                    cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
                // 管理费
                else if (cost.getCode().equals("管理费")) {
                    BusBudgetAccountingBasicInfoConfigModel coefficientCondition =
                            new BusBudgetAccountingBasicInfoConfigModel();
                    coefficientCondition.setBatId(cost.getBatId());
                    coefficientCondition.setCode("Management rate");
                    BusBudgetAccountingBasicInfoConfigModel coefficient =
                            accountingDao.getBusBudgetAccountingBasicInfoConfigByCondition(coefficientCondition);
                    cost.setRemark("管理费率：" + coefficient.getValue() + "%");
                    // 获取小计
                    BusBudgetAccountingCostModel subtotalCondition = new BusBudgetAccountingCostModel();
                    subtotalCondition.setBatId(cost.getBatId());
                    subtotalCondition.setCode("小计");
                    subtotalCondition.setVerId(5);
                    BusBudgetAccountingCostModel subtotalCost =
                            accountingDao.getSubtotalCostByCondition(subtotalCondition);
                    cost.setTotalPrice(
                            subtotalCost
                                    .getTotalPrice()
                                    .multiply(
                                            new BigDecimal(coefficient.getValue())
                                                    .divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP)));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                subtotalCost
                                        .getTotalPrice()
                                        .multiply(
                                                new BigDecimal(coefficient.getValue())
                                                        .divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP))
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                subtotalCost
                                        .getTotalPrice()
                                        .multiply(
                                                new BigDecimal(coefficient.getValue())
                                                        .divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP))
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
                // 回写合计
                else if (cost.getCode().equals("合计")) {
                    //总净重
//                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new BusGeneralHoursModel();
//                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
//                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
//                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
//                    carbonSteelTotalNetWeightCondition.setTypes("1,2,3");
//                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
//                    BigDecimal totalNetWeight =
//                            getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);

                    BusGeneralHours calculationTotalWeightCondition = new BusGeneralHours();
                    calculationTotalWeightCondition.setBatId(cost.getBatId());
                    calculationTotalWeightCondition.setTypes("1,2,3");
                    calculationTotalWeightCondition.setLaborTypes("3");
                    BigDecimal calculationTotalWeight =accountingDao.getCalculationTotalWeightByCondition(calculationTotalWeightCondition);
                    cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
                    if (cost.getAmountDisplayModel().intValue() == 1) {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .setScale(0, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    } else {
                        cost.setDisplayTotalPrice(
                                accountingDao
                                        .getTotalPriceByBatIdAndParentCode(cost)
                                        .divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());
                    }
                    if (calculationTotalWeight.compareTo(new BigDecimal(0)) == 1) {
                        cost.setSyntheticalPrice(
                                cost.getTotalPrice().divide(calculationTotalWeight.divide(new BigDecimal(project.getNum()), 8, BigDecimal.ROUND_HALF_UP), 8, BigDecimal.ROUND_HALF_UP));
                        if (calculationTotalWeight.compareTo(new BigDecimal(0)) == 1) {
                            if (cost.getAmountDisplayModel().intValue() == 1) {
                                cost.setRemark(
                                        "综合单价："
                                                + cost.getSyntheticalPrice().setScale(0, BigDecimal.ROUND_HALF_UP)
                                                + "元/吨");
                            } else {
                                cost.setRemark(
                                        "综合单价："
                                                + cost.getSyntheticalPrice().divide(divisor, 2, BigDecimal.ROUND_HALF_UP)
                                                + "万元/吨");
                            }
                        } else {
                            if (cost.getAmountDisplayModel().intValue() == 1) {
                                cost.setRemark("");
                            } else {
                                cost.setRemark("");
                            }
                        }
                    }

                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
                }
            }
            //            //对查看 审核 打印成本清单进行核算
            //            BusBudgetAccountingCostModel showCostListCondition = new
            // BusBudgetAccountingCostModel();
            //            showCostListCondition.setBatId(order.getId());
            //            showCostListCondition.setVerId(1);
            //            List<BusBudgetAccountingCostModel> showCostList =
            // getDataDao.findCostListByCondition(showCostListCondition);
            //            for (int i = 0; i < showCostList.size(); i++) {
            //                BusBudgetAccountingCostModel cost = showCostList.get(i);
            //                //设置被除数
            //                BigDecimal divisor = new BigDecimal(0);
            //                switch (cost.getAmountDisplayModel().intValue()) {
            //                    case 1:
            //                        divisor = new BigDecimal(1);
            //                        break;
            //                    case 2:
            //                        divisor = new BigDecimal(10000);
            //                        break;
            //                    case 3:
            //                        divisor = new BigDecimal(1000000);
            //                        break;
            //                    default:
            //                        divisor = new BigDecimal(100000000);
            //                        break;
            //                }
            //                if (cost.getCode().contains("-材料费-材料采购费-碳钢") ||
            //                        cost.getCode().contains("-材料费-材料采购费-不锈钢") ||
            //                        cost.getCode().contains("-材料费-材料采购费-铝材")) {
            //                    //获取碳钢总料重
            //                    BusBudgetAccountingEstimatesData carbonSteelTotalWeightCondition = new
            // BusBudgetAccountingEstimatesData();
            //                    carbonSteelTotalWeightCondition.setBatId(cost.getBatId());
            //                    carbonSteelTotalWeightCondition.setProCode(order.getProCode());
            //                    carbonSteelTotalWeightCondition.setUseType(2);
            //                    if (cost.getCode().contains("-材料费-材料采购费-碳钢")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费碳钢总料重");
            //                    } else if (cost.getCode().contains("-材料费-材料采购费-不锈钢")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费不锈钢总料重");
            //                    } else if (cost.getCode().contains("-材料费-材料采购费-铝材")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费铝材总料重");
            //                    }
            //                    carbonSteelTotalWeightCondition.setDataMajor(cost.getMajor());
            ////                    carbonSteelTotalWeightCondition.setCostId(cost.getId());
            //                    BusBudgetAccountingEstimatesData totalWeightObj =
            // getDataDao.getBusBudgetAccountingEstimatesDataByCondition(carbonSteelTotalWeightCondition);
            //                    //获取总料重
            //                    String totalWeightStr = totalWeightObj == null ? "0.00" :
            // totalWeightObj.getDataValue();
            //                    BigDecimal totalWeight = new BigDecimal(totalWeightStr);
            //                    //获取总价
            //                    if (cost.getCode().contains("-材料费-材料采购费-碳钢")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费碳钢总价");
            //                    } else if (cost.getCode().contains("-材料费-材料采购费-不锈钢")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费不锈钢总价");
            //                    } else if (cost.getCode().contains("-材料费-材料采购费-铝材")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费铝材总价");
            //                    }
            //                    BusBudgetAccountingEstimatesData totalPrice =
            // getDataDao.getBusBudgetAccountingEstimatesDataByCondition(carbonSteelTotalWeightCondition);
            //                    if (totalPrice != null) {
            //                        cost.setTotalPrice(new
            // BigDecimal(totalPrice.getDataValue()).divide(new BigDecimal(project.getNum()), 10,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayTotalPrice(new
            // BigDecimal(totalPrice.getDataValue()).divide(new BigDecimal(project.getNum())).setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayTotalPrice(new
            // BigDecimal(totalPrice.getDataValue()).divide(new
            // BigDecimal(project.getNum())).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    //获取单价
            //                    if (cost.getCode().contains("-材料费-材料采购费-碳钢")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费碳钢单价");
            //                    } else if (cost.getCode().contains("-材料费-材料采购费-不锈钢")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费不锈钢单价");
            //                    } else if (cost.getCode().contains("-材料费-材料采购费-铝材")) {
            //                        carbonSteelTotalWeightCondition.setDataType("该项目材料费铝材单价");
            //                    }
            //                    BusBudgetAccountingEstimatesData price =
            // getDataDao.getBusBudgetAccountingEstimatesDataByCondition(carbonSteelTotalWeightCondition);
            //                    if (price != null) {
            //                        cost.setPrice(new BigDecimal(totalPrice.getDataValue()).divide(new
            // BigDecimal(project.getNum()), 10, BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayTotalPrice(new
            // BigDecimal(totalPrice.getDataValue()).divide(new BigDecimal(project.getNum()), 10,
            // BigDecimal.ROUND_HALF_UP).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayTotalPrice(new
            // BigDecimal(totalPrice.getDataValue()).divide(new BigDecimal(project.getNum()), 10,
            // BigDecimal.ROUND_HALF_UP).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    //获取总净重
            //                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new
            // BusGeneralHoursModel();
            //                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
            //                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
            //                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
            //                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
            //                    BigDecimal recoveryPrice = new BigDecimal(0);
            //                    if (cost.getCode().contains("-碳钢")) {
            //                        recoveryPrice = carbonSteelRecovery;
            //                    } else if (cost.getCode().contains("-不锈钢")) {
            //                        recoveryPrice = stainlessSteelRecycling;
            //                    }
            //                    if (cost.getMajor().equals("机械")) {
            //                        carbonSteelTotalNetWeightCondition.setTypes("1");
            //                    } else if (cost.getMajor().equals("流体")) {
            //                        carbonSteelTotalNetWeightCondition.setTypes("2");
            //                    } else if (cost.getMajor().equals("自动化")) {
            //                        carbonSteelTotalNetWeightCondition.setTypes("3");
            //                    }
            //                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
            //                    BigDecimal totalNetWeight =
            // getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
            //                    if (totalWeight.compareTo(new BigDecimal(0)) == 1) {
            //                        //获取废料回收系数
            //                        cost.setRemark("净重：" + Double.valueOf(totalNetWeight.toString()) +
            // "吨，损耗：" + Double.valueOf(totalWeight.subtract(totalNetWeight).divide(totalWeight, 10,
            // BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).toString()) + "%");
            //                    } else {
            //                        //获取废料回收系数
            //                        cost.setRemark("净重：0吨，损耗：0%");
            //                    }
            //                    cost.setNum(totalWeight);
            //                    cost.setDisplayNum(totalWeight.divide(new BigDecimal(project.getNum()),
            // 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                    //获取对应的废料回收费
            //                    BusBudgetAccountingCostModel wasteCost = new
            // BusBudgetAccountingCostModel();
            //                    BeanUtil.copy(cost, wasteCost);
            //                    wasteCost.setCode(wasteCost.getCode().replace("-材料采购费-", "-废料回收费-"));
            //                    wasteCost.setVerId(1);
            //                    wasteCost =
            // accountingDao.getBusBudgetAccountingCostByCondition2(wasteCost);
            //
            // wasteCost.setNum(totalWeight.subtract(totalNetWeight).multiply(coefficientWasteRecovery).divide(new BigDecimal(project.getNum()), 10, BigDecimal.ROUND_HALF_UP));
            //
            // wasteCost.setDisplayNum(totalWeight.subtract(totalNetWeight).multiply(coefficientWasteRecovery).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getCode().contains("-碳钢") || cost.getCode().contains("-不锈钢")) {
            //                        wasteCost.setPrice(recoveryPrice);
            //                        if (wasteCost.getAmountDisplayModel().intValue() == 1) {
            //                            wasteCost.setDisplayPrice(recoveryPrice.setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            wasteCost.setDisplayPrice(recoveryPrice.divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //
            // wasteCost.setTotalPrice(wasteCost.getPrice().multiply(wasteCost.getNum()));
            //                    if (wasteCost.getAmountDisplayModel().intValue() == 1) {
            //
            // wasteCost.setDisplayTotalPrice(wasteCost.getPrice().multiply(wasteCost.getNum()).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // wasteCost.setDisplayTotalPrice(wasteCost.getPrice().multiply(wasteCost.getNum()).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //
            // accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(wasteCost);
            //                } else if (cost.getCode().contains("-材料费-材料采购费-其他")) {
            //                    //获取父级项
            //                    BusBudgetAccountingCostModel matPurFeeCost =
            // accountingDao.getBusBudgetAccountingCostByBatIdAndParentCode(cost);
            //                    //然后把该父级项所以子集项相加的值加在父级项上
            //                    //获取总净重
            //                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new
            // BusGeneralHoursModel();
            //                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
            //                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
            //                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
            //                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
            //                    if (cost.getMajor().equals("机械")) {
            //                        carbonSteelTotalNetWeightCondition.setTypes("1");
            //                    } else if (cost.getMajor().equals("流体")) {
            //                        carbonSteelTotalNetWeightCondition.setTypes("2");
            //                    } else if (cost.getMajor().equals("自动化")) {
            //                        carbonSteelTotalNetWeightCondition.setTypes("3");
            //                    }
            //                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
            //                    BigDecimal totalNetWeight =
            // getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
            //                    //获取子集集合去掉其他
            //                    matPurFeeCost.setVerId(1);
            //                    List<BusBudgetAccountingCostModel> childList =
            // getDataDao.getChildListByCondition(matPurFeeCost);
            //                    BigDecimal totalLoss = new BigDecimal(0);
            //                    for (BusBudgetAccountingCost childCost : childList) {
            //                        if (!childCost.getCode().contains("其他") && childCost.getRemark() !=
            // null) {
            //                            String lossStr =
            // childCost.getRemark().substring(childCost.getRemark().indexOf("吨，损耗：") + 5,
            // childCost.getRemark().indexOf("%"));
            //                            BigDecimal loss = new BigDecimal(lossStr);
            //                            totalLoss = totalLoss.add(loss);
            //                        }
            //                    }
            //
            //                    matPurFeeCost.setRemark("净重：" +
            // Double.valueOf(totalNetWeight.toString()) + "吨，损耗：" + Double.valueOf(totalLoss.toString())
            // + "%");
            //
            // matPurFeeCost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(matPurFeeCost));
            //                    if (matPurFeeCost.getAmountDisplayModel().intValue() == 1) {
            //
            // matPurFeeCost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(matPurFeeCost).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // matPurFeeCost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(matPurFeeCost).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //
            // accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(matPurFeeCost);
            //                }
            //                //回写废料回收费 和 材料费
            //                else if (cost.getCode().contains("-材料费-废料回收费-其他")) {
            //                    //获取父级项
            //                    BusBudgetAccountingCostModel WasteRecyclingFeeCost =
            // accountingDao.getBusBudgetAccountingCostByBatIdAndParentCode(cost);
            //                    //然后把该父级项所以子集项相加的值加在父级项上
            //
            // WasteRecyclingFeeCost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(WasteRecyclingFeeCost));
            //                    if (WasteRecyclingFeeCost.getAmountDisplayModel().intValue() == 1) {
            //
            // WasteRecyclingFeeCost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(WasteRecyclingFeeCost).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // WasteRecyclingFeeCost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(WasteRecyclingFeeCost).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    WasteRecyclingFeeCost.setRemark("废料总重：" +
            // Double.valueOf(accountingDao.getTotalNumByBatIdAndParentCode(WasteRecyclingFeeCost).toString()) + "吨");
            //
            // accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(WasteRecyclingFeeCost);
            //                    //获取父级项 材料费
            //                    BusBudgetAccountingCost matFeeCost =
            // accountingDao.getBusBudgetAccountingCostByBatIdAndParentCode(WasteRecyclingFeeCost);
            //                    //获取材料采购费
            //                    BusBudgetAccountingCostModel matPurFeeCondition = new
            // BusBudgetAccountingCostModel();
            //                    matPurFeeCondition.setBatId(matFeeCost.getBatId());
            //                    matPurFeeCondition.setMajor(matFeeCost.getMajor());
            //                    matPurFeeCondition.setPart(matFeeCost.getPart());
            //                    matPurFeeCondition.setParentCode(matFeeCost.getCode());
            //                    matPurFeeCondition.setCode("-材料费-材料采购费");
            //                    matPurFeeCondition.setVerId(1);
            //                    BusBudgetAccountingCost matPurFeeCost =
            // accountingDao.getBusBudgetAccountingCostByParentCodeAndCondition(matPurFeeCondition);
            //                    if (WasteRecyclingFeeCost.getTotalPrice() != null) {
            //
            // matFeeCost.setTotalPrice(matPurFeeCost.getTotalPrice().subtract(WasteRecyclingFeeCost.getTotalPrice()));
            //                        if (matFeeCost.getAmountDisplayModel().intValue() == 1) {
            //
            // matFeeCost.setDisplayTotalPrice(matPurFeeCost.getTotalPrice().subtract(WasteRecyclingFeeCost.getTotalPrice()).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //
            // matFeeCost.setDisplayTotalPrice(matPurFeeCost.getTotalPrice().subtract(WasteRecyclingFeeCost.getTotalPrice()).toString());
            //                        }
            //                    } else {
            //                        matFeeCost.setTotalPrice(new BigDecimal(0));
            //                        matFeeCost.setDisplayTotalPrice("0");
            //                    }
            //
            // accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(matFeeCost);
            //                }
            //                //回写制造费
            //                else if (cost.getCode().contains("-制造费") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-制造费")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-制造费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    //获取总净重
            //                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new
            // BusGeneralHoursModel();
            //                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
            //                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
            //                    carbonSteelTotalNetWeightCondition.setPart(cost.getPart());
            //                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
            //                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
            //                    carbonSteelTotalNetWeightCondition.setTypes("1,2,3");
            //                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
            //                    BigDecimal totalNetWeight =
            // getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
            //
            //                    if (totalNetWeight.compareTo(new BigDecimal(0)) == 1) {
            //                        BigDecimal tonPriceBigDecimal =
            // cost.getTotalPrice().divide(totalNetWeight, 10, BigDecimal.ROUND_HALF_UP).divide(new
            // BigDecimal(10000), 10, BigDecimal.ROUND_HALF_UP);
            //                        cost.setRemark("制造单价：" +
            // Double.valueOf(tonPriceBigDecimal.toString()) + "万元/吨");
            //                    } else {
            //                        cost.setRemark("制造单价：0.00万元/吨");
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写下料
            //                else if (cost.getCode().contains("-制造费-下料") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-下料")) {
            //                    BusBudgetAccountingCostModel cutCondition = new
            // BusBudgetAccountingCostModel();
            //                    cutCondition.setBatId(cost.getBatId());
            //                    cutCondition.setMajor(cost.getMajor());
            //                    cutCondition.setParentCode(cost.getMajor() + "%-制造费");
            //                    cutCondition.setCode(cost.getMajor() + "%-制造费-下料");
            //                    cutCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(cutCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(cutCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写铆焊
            //                else if (cost.getCode().contains("-制造费-铆焊") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-铆焊")) {
            //                    BusBudgetAccountingCostModel cutCondition = new
            // BusBudgetAccountingCostModel();
            //                    cutCondition.setBatId(cost.getBatId());
            //                    cutCondition.setMajor(cost.getMajor());
            //                    cutCondition.setParentCode(cost.getMajor() + "%-制造费");
            //                    cutCondition.setCode(cost.getMajor() + "%-制造费-铆焊");
            //                    cutCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(cutCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(cutCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写机加
            //                else if (cost.getCode().contains("-制造费-机加") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-机加")) {
            //                    BusBudgetAccountingCostModel cutCondition = new
            // BusBudgetAccountingCostModel();
            //                    cutCondition.setBatId(cost.getBatId());
            //                    cutCondition.setMajor(cost.getMajor());
            //                    cutCondition.setParentCode(cost.getMajor() + "%-制造费");
            //                    cutCondition.setCode(cost.getMajor() + "%-制造费-机加");
            //                    cutCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(cutCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(cutCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写喷涂
            //                else if (cost.getCode().contains("-制造费-喷涂") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-喷涂")) {
            //                    BusBudgetAccountingCostModel cutCondition = new
            // BusBudgetAccountingCostModel();
            //                    cutCondition.setBatId(cost.getBatId());
            //                    cutCondition.setMajor(cost.getMajor());
            //                    cutCondition.setParentCode(cost.getMajor() + "%-制造费");
            //                    cutCondition.setCode(cost.getMajor() + "%-制造费-喷涂");
            //                    cutCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(cutCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(cutCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写装配
            //                else if (cost.getCode().contains("-制造费-装配") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-装配")) {
            //                    BusBudgetAccountingCostModel cutCondition = new
            // BusBudgetAccountingCostModel();
            //                    cutCondition.setBatId(cost.getBatId());
            //                    cutCondition.setMajor(cost.getMajor());
            //                    cutCondition.setParentCode(cost.getMajor() + "%-制造费");
            //                    cutCondition.setCode(cost.getMajor() + "%-制造费-装配");
            //                    cutCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //
            //                    cost.setNum(accountingDao.getNumByPurCondition(cutCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(cutCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写热处理
            //                else if (cost.getCode().contains("-制造费-热处理") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-热处理")) {
            //                    BusBudgetAccountingCostModel cutCondition = new
            // BusBudgetAccountingCostModel();
            //                    cutCondition.setBatId(cost.getBatId());
            //                    cutCondition.setMajor(cost.getMajor());
            //                    cutCondition.setParentCode(cost.getMajor() + "%-制造费");
            //                    cutCondition.setCode(cost.getMajor() + "%-制造费-热处理");
            //                    cutCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(cutCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(cutCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(cutCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写外购费
            //                else if (cost.getCode().contains("-外购费") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-外购费")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-外购费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写外协费
            //                else if (cost.getCode().contains("-外协费") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-外协费")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-外协费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写整体外协费
            //                else if (cost.getCode().contains("-整体外协") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-整体外协")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-外协费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写带料外协费
            //                else if (cost.getCode().contains("-带料外协") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-带料外协")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-外协费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写现场安装费
            //                else if (cost.getCode().contains("-现场安装费") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-现场安装费")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    //回写现场安装费
            //                    //获取总净重
            //                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new
            // BusGeneralHoursModel();
            //                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
            //                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
            //                    carbonSteelTotalNetWeightCondition.setMajor(cost.getMajor());
            //                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
            //                    carbonSteelTotalNetWeightCondition.setTypes("1,2,3");
            //                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
            //                    BigDecimal totalNetWeight =
            // getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
            //                    if (totalNetWeight.compareTo(new BigDecimal(0)) == 1) {
            //                        BigDecimal tonPriceBigDecimal =
            // cost.getTotalPrice().divide(totalNetWeight, 10, BigDecimal.ROUND_HALF_UP).divide(new
            // BigDecimal(10000), 10, BigDecimal.ROUND_HALF_UP);
            //                        cost.setRemark("安装单价：" +
            // Double.valueOf(tonPriceBigDecimal.toString()) + "万元/吨");
            //                    } else {
            //                        cost.setRemark("安装单价：0.00万元/吨");
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写人工
            //                else if (cost.getCode().contains("-现场安装费-人工") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-人工")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor() + "%-现场安装费");
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费-人工");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写安装人工
            //                else if (cost.getCode().contains("-现场安装费-人工-安装人工") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-安装人工")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor() + "%-现场安装费-人工");
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费-人工-安装人工");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写调试人工
            //                else if (cost.getCode().contains("-现场安装费-人工-调试人工") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-调试人工")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor() + "%-现场安装费-人工");
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费-人工-调试人工");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    if (cost.getNum().compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setPrice(cost.getTotalPrice().divide(cost.getNum(), 2,
            // BigDecimal.ROUND_HALF_UP));
            //                        if (cost.getAmountDisplayModel().intValue() == 1) {
            //                            cost.setDisplayPrice(cost.getPrice().setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //                            cost.setDisplayPrice(cost.getPrice().divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                } else if (cost.getCode().contains("-现场安装费-辅材") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-辅材")) {
            //                    cost.setRemark("安装人工");
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor() + "%-现场安装费");
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费-辅材");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                } else if (cost.getCode().contains("-现场安装费-食宿") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-食宿")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor() + "%-现场安装费");
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费-食宿");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    cost.setPrice(accountingDao.getPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayPrice(accountingDao.getPriceByPurCondition(purCondition).setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayPrice(accountingDao.getPriceByPurCondition(purCondition).divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                } else if (cost.getCode().contains("-现场安装费-差旅") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-差旅")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor() + "%-现场安装费");
            //                    purCondition.setCode(cost.getMajor() + "%-现场安装费-差旅");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    cost.setNum(accountingDao.getNumByPurCondition(purCondition));
            //
            // cost.setDisplayNum(accountingDao.getNumByPurCondition(purCondition).setScale(2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    cost.setPrice(accountingDao.getPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayPrice(accountingDao.getPriceByPurCondition(purCondition).setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayPrice(accountingDao.getPriceByPurCondition(purCondition).divide(divisor, 2,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写部分
            //                else if (cost.getCode().contains("-设计费") &&
            // cost.getCode().substring(cost.getCode().lastIndexOf("-")).equals("-设计费")) {
            //                    BusBudgetAccountingCostModel purCondition = new
            // BusBudgetAccountingCostModel();
            //                    purCondition.setBatId(cost.getBatId());
            //                    purCondition.setMajor(cost.getMajor());
            //                    purCondition.setParentCode(cost.getMajor());
            //                    purCondition.setCode(cost.getMajor() + "%-设计费");
            //                    purCondition.setVerId(5);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByPurCondition(purCondition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写专业和小计
            //                else if (cost.getCode().equals("小计")) {
            //                    List<BusBudgetAccountingCostModel> majorCostList =
            // accountingDao.findBusBudgetAccountingCostByParentCodeAndBatId(cost);
            //                    for (BusBudgetAccountingCostModel majorCost : majorCostList) {
            //
            // majorCost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(majorCost));
            //                        if (majorCost.getAmountDisplayModel().intValue() == 1) {
            //
            // majorCost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(majorCost).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //
            // majorCost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(majorCost).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //
            // accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(majorCost);
            //                    }
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost).setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写包装费
            //                else if (cost.getCode().equals("包装运输费")) {
            //                    //获取子项包装费运输费
            //                    List<BusBudgetAccountingCostModel> childCosts =
            // accountingDao.findBusBudgetAccountingCostByParentCodeAndBatId(cost);
            //                    for (BusBudgetAccountingCostModel costModel : childCosts ) {
            //                        BusBudgetAccountingCostModel condition = new
            // BusBudgetAccountingCostModel();
            //                        BeanUtil.copy(costModel,condition);
            //                        condition.setVerId(5);
            //                        //总价
            //
            // costModel.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(condition));
            //                        if (costModel.getAmountDisplayModel().intValue() == 1) {
            //
            // costModel.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(condition).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                        } else {
            //
            // costModel.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(condition).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                        }
            //                        //数量
            //                        costModel.setNum(accountingDao.getNumByPurCondition(condition));
            //                    }
            //
            //
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost).setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //管理费
            //                else if (cost.getCode().equals("管理费")) {
            //                    BusBudgetAccountingBasicInfoConfigModel coefficientCondition = new
            // BusBudgetAccountingBasicInfoConfigModel();
            //                    coefficientCondition.setBatId(cost.getBatId());
            //                    coefficientCondition.setCode("Management rate");
            //                    BusBudgetAccountingBasicInfoConfigModel coefficient =
            // accountingDao.getBusBudgetAccountingBasicInfoConfigByCondition(coefficientCondition);
            //                    cost.setRemark("管理费率：" + new
            // BigDecimal(coefficient.getValue()).multiply(new BigDecimal(100)) + "%");
            //                    //获取小计
            //                    BusBudgetAccountingCostModel subtotalCondition = new
            // BusBudgetAccountingCostModel();
            //                    subtotalCondition.setBatId(cost.getBatId());
            //                    subtotalCondition.setCode("小计");
            //                    subtotalCondition.setVerId(1);
            //                    BusBudgetAccountingCostModel subtotalCost =
            // accountingDao.getSubtotalCostByCondition(subtotalCondition);
            //                    cost.setTotalPrice(subtotalCost.getTotalPrice().multiply(new
            // BigDecimal(coefficient.getValue()).divide(new BigDecimal(100), 2,
            // BigDecimal.ROUND_HALF_UP)));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //                        cost.setDisplayTotalPrice(subtotalCost.getTotalPrice().multiply(new
            // BigDecimal(coefficient.getValue()).divide(new BigDecimal(100), 2,
            // BigDecimal.ROUND_HALF_UP)).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //                        cost.setDisplayTotalPrice(subtotalCost.getTotalPrice().multiply(new
            // BigDecimal(coefficient.getValue()).divide(new BigDecimal(100), 2,
            // BigDecimal.ROUND_HALF_UP)).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //                //回写合计
            //                else if (cost.getCode().equals("合计")) {
            //                    BusGeneralHoursModel carbonSteelTotalNetWeightCondition = new
            // BusGeneralHoursModel();
            //                    carbonSteelTotalNetWeightCondition.setBatId(cost.getBatId());
            //                    carbonSteelTotalNetWeightCondition.setProCode(order.getProCode());
            //                    carbonSteelTotalNetWeightCondition.setDataUseType(2);
            //                    carbonSteelTotalNetWeightCondition.setTypes("1,2,3");
            //                    carbonSteelTotalNetWeightCondition.setLaborTypes("2");
            //                    BigDecimal totalNetWeight =
            // getDataDao.getTotalNumByCondition(carbonSteelTotalNetWeightCondition);
            //
            // cost.setTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost));
            //                    if (cost.getAmountDisplayModel().intValue() == 1) {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost).setScale(0,
            // BigDecimal.ROUND_HALF_UP).toString());
            //                    } else {
            //
            // cost.setDisplayTotalPrice(accountingDao.getTotalPriceByBatIdAndParentCode(cost).divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
            //                    }
            //                    if (totalNetWeight.compareTo(new BigDecimal(0)) == 1) {
            //                        cost.setRemark("综合单价：" +
            // Double.valueOf(cost.getTotalPrice().divide(totalNetWeight, 10,
            // BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(10000), 2,
            // BigDecimal.ROUND_HALF_UP).toString()) + "万元/吨");
            //                    } else {
            //                        cost.setRemark("综合单价：0.00万元/吨");
            //                    }
            //                    accountingDao.updateBusBudgetAccountingCostSelectiveByPrimaryKey(cost);
            //                }
            //            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("计算完毕");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("计算失败");
        }
        return jsonResult;
    }

    /**
     * getCostInfo
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/27 16:12
     * @version 1.0
     */
    public JsonResult getCostInfo(BusBudgetAccountingCostModel param) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setData(accountingDao.getBusBudgetAccountingCostByPramaryKey(param.getId()));
        return jsonResult;
    }

    /**
     * saveCostItem 保存成本项信息
     *
     * @param param
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/31 8:51
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult saveCostItem(BusBudgetAccountingCostModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            BusBudgetAccountingCostModel model =
                    accountingDao.getBusBudgetAccountingCostByPramaryKey(param.getId());
            // 设置被除数
            BigDecimal divisor = new BigDecimal(0);
            switch (model.getAmountDisplayModel().intValue()) {
                case 1:
                    divisor = new BigDecimal(1);
                    break;
                case 2:
                    divisor = new BigDecimal(10000);
                    break;
                case 3:
                    divisor = new BigDecimal(1000000);
                    break;
                default:
                    divisor = new BigDecimal(100000000);
                    break;
            }

            BeanUtil.copy(param, model);
            if (model.getNum() != null && model.getNum().compareTo(new BigDecimal(0)) == 1) {
                model.setDisplayNum(model.getNum().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            } else if (model.getNum() != null && model.getNum().compareTo(new BigDecimal(0)) == 0) {
                model.setNum(null);
                model.setDisplayNum(null);
            } else {
                model.setNum(null);
                model.setDisplayNum(null);
            }
            if (model.getPrice() != null && model.getPrice().compareTo(new BigDecimal(0)) == 1) {
                if (model.getAmountDisplayModel().intValue() == 1) {
                    model.setDisplayPrice(model.getPrice().toString());
                } else {
                    model.setDisplayPrice(
                            model.getPrice().divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
                }
            } else if (model.getPrice() != null && model.getPrice().compareTo(new BigDecimal(0)) == 0) {
                model.setPrice(null);
                model.setDisplayPrice(null);
            } else {
                model.setPrice(null);
                model.setDisplayPrice(null);
            }
            if (model.getTotalPrice() == null
                    && model.getNum() != null
                    && model.getNum().compareTo(new BigDecimal(0)) == 1
                    && model.getPrice() != null
                    && model.getPrice().compareTo(new BigDecimal(0)) == 1) {
                model.setTotalPrice(model.getNum().multiply(model.getPrice()));
                if (model.getAmountDisplayModel().intValue() == 1) {
                    model.setDisplayTotalPrice(model.getTotalPrice().toString());
                } else {
                    model.setDisplayTotalPrice(
                            model.getTotalPrice().divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
                }
            } else if (model.getNum() != null
                    && model.getNum().compareTo(new BigDecimal(0)) == 0
                    && model.getPrice() != null
                    && model.getPrice().compareTo(new BigDecimal(0)) == 0) {
                model.setTotalPrice(null);
                model.setDisplayTotalPrice(null);
            } else if (model.getTotalPrice() != null) {
                if (model.getAmountDisplayModel().intValue() == 1) {
                    model.setDisplayTotalPrice(
                            model.getTotalPrice().divide(divisor, 0, BigDecimal.ROUND_HALF_UP).toString());
                } else {
                    model.setDisplayTotalPrice(
                            model.getTotalPrice().divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
                }
            } else if (model.getTotalPrice() == null
                    && model.getNum() == null
                    && model.getPrice() == null) {
                model.setTotalPrice(null);
                model.setDisplayTotalPrice(null);
            }
            if (model.getTrueAmount() != null) {
                if (model.getAmountDisplayModel().intValue() == 1) {
                    model.setDisplayTrueAmount(model.getTrueAmount().divide(divisor, 0, BigDecimal.ROUND_HALF_UP).toString());
                } else {
                    model.setDisplayTrueAmount(model.getTrueAmount().divide(divisor, 2, BigDecimal.ROUND_HALF_UP).toString());
                }
            }
            accountingDao.updateBusBudgetAccountingCostByParamPrimaryKey(model);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败！");
        }
        return jsonResult;
    }

    /**
     * regenerateCostSheet 重新生成核算清单
     *
     * @param params 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/31 15:00
     * @version 1.0
     */
    public JsonResult regenerateCostSheet(BusBudgetAccountingCost params) {
        JsonResult jsonResult = new JsonResult();
        try {
            getDataDao.deleteBusBudgetAccountingCostByBatId(params.getBatId());
            getDataService.generateCostItems(params.getBatId().toString());
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("重新生成成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("重新生成失败！");
        }
        return jsonResult;
    }

    /**
     * saveAccountingCost 保存核算清单
     *
     * @param param
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/10 18:39
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult saveAccountingCost(BusBudgetAccountingTaskModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            // 保存当前清单总计到单据表的金额字段
            BusBudgetAccountingCostModel condition = new BusBudgetAccountingCostModel();
            condition.setBatId(param.getId());
            condition.setCode("合计");
            BigDecimal total =
                    getDataDao.getBusBudgetAccountingCostModelByBatIdAndCode(condition).getTotalPrice();
            param.setAmount(total);
            param.setIsSave(1);
            getDataDao.updateBusBudgetAccountingTaskByPrimaryKeySelective(param);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败！");
        }
        return jsonResult;
    }

    /**
     * saveEstimatingCost 保存概算清单
     *
     * @param param
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2021/1/7 20:14
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult saveEstimatingCost(BusBudgetAccountingTaskModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            // 保存当前清单总计到单据表的金额字段
            BusBudgetAccountingCostModel condition = new BusBudgetAccountingCostModel();
            condition.setBatId(param.getId());
            condition.setCode("合计");
            BigDecimal total = getDataDao.getBusBudgetAccountingCostModelByBatIdAndCode(condition).getTotalPrice();
            param.setAmount(total);
            param.setIsSave(1);
            getDataDao.updateBusBudgetAccountingTaskByPrimaryKeySelective(param);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败！");
        }
        return jsonResult;
    }

    /**
     * findSubmitList 获取核算成本清单提交列表
     *
     * @param param 检索条件
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel
     * 列表数据
     * @author 徐一贺
     * @date 2020/11/10 20:14
     * @version 1.0
     */
    public List<AccountingProjectModel> findSubmitList(AccountingProjectModel param) {
        param.setCurrentUserId(ShiroUtils.getUserId());
        return getDataDao.findSubmitList(param);
    }

    /**
     * findBusinessGroupList 获取非删除的组名
     *
     * @return java.util.List<com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusinessGroupModel>
     * @author 徐一贺
     * @date 2020/11/23 13:24
     */
    public List<BusinessGroupModel> findBusinessGroupList() {
        return getDataDao.findBusinessGroupList();
    }

    /**
     * findBusBudgetAccountingCostByBatId 根据单据表主键获取成本清单
     *
     * @param batId 单据表主键
     * @return java.util.List<com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusBudgetAccountingCostModel>
     * @throws
     * @author 徐一贺
     * @date 2020/11/23 14:57
     */
    public List<BusBudgetAccountingCost> findShowBusBudgetAccountingCostByBatId(Integer batId) {
        return getDataDao.findShowBusBudgetAccountingCostByBatId(batId);
    }

    /**
     * getBusBudgetAccountingTaskByPrimaryKey 根据主键获取单据表
     *
     * @param batId 主键
     * @return com.zhongwang.business.gen.entity.BusBudgetAccountingTask
     * @throws
     * @author 徐一贺
     * @date 2020/11/23 15:09
     */
    public BusBudgetAccountingTaskModel getBusBudgetAccountingTaskByPrimaryKey(Integer batId) {
        return getDataDao.getBusBudgetAccountingTaskByPrimaryKey(batId);
    }

    /**
     * accountingSubmit 核算清单提交
     *
     * @param id 核算子集单据表主键
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/24 15:54
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult accountingSubmit(Integer id) {
        JsonResult jsonResult = new JsonResult();
        BusBudgetAccountingTaskModel order = getDataDao.getBusBudgetAccountingTaskByPrimaryKey(id);
        Date currentTime = new Date();
        User user = ShiroUtils.getUser();
        try {
            // 首先修改核算的状态
            BusBudgetAccountingTaskModel accountingTaskModel = new BusBudgetAccountingTaskModel();
            accountingTaskModel.setId(id);
            /** 0=未提交，1=已提交/提交待审，2=审核通过，3=被驳回 */
            accountingTaskModel.setSubmitStatus(1);
            accountingTaskModel.setSubmitDate(currentTime);
            getDataDao.updateBusBudgetAccountingTaskByPrimaryKeySelective(accountingTaskModel);
            // 获取单据表如果单据表不存在则插入单据表信息
            AuditProcessModel orderSheet = getDataDao.getSheetDetailModelByBllId(id, 6);
            if (orderSheet != null) {
                if (orderSheet.getAppStatus() != 0) {
                    // 改变审核状态生成下一个审核流程记录
                }
            }
            // 创建审核流程
            else {
                orderSheet = new AuditProcessModel();
                orderSheet.setBllId(id);
                orderSheet.setSubmitUser(user.getId());
                orderSheet.setSubmitTime(currentTime);
                orderSheet.setAppType(6);
                getDataDao.insertSheetDetail(orderSheet);
            }

            // 生成下一步待审核的记录************************
            // 01 首先需要查询对应的流程信息
            AuditProcessModel addAppInfo = new AuditProcessModel();
            /** 执行成功以后，获取数据的主键 */
            addAppInfo.setSheetId(orderSheet.getId());
            addAppInfo.setCode("HS001");
            /** 0=被驳回，1=未审核，2=已审核 */
            addAppInfo.setAppStatus(1);

            // 循环判断 当前的账号所在的流程节点；
            List<AuditProcessModel> nowUserNodeInfo = getDataDao.findAppNodeInfoByUserId(user.getId());
            // 分2种情况
            if (nowUserNodeInfo == null || nowUserNodeInfo.size() == 0) {
                // 无数据；1·没有再审批节点中，是员工级别的，那么直接就按照流程顺序走。
                List<AuditProcessModel> appList = getDataDao.findAppNodeInfo(0);
                // 然后在找下一个节点和下下节点。
                /** 上一个节点是0 */
                addAppInfo.setComp(0);
                /** 判断流程节点，是1个还是2个及以上。 */
                if (appList != null && appList.size() == 1) {
                    // 获取第一条数据，为当前的节点号，执行人。
                    // 获取第二条数据的，节点。
                    addAppInfo.setNowNode(appList.get(0).getId());
                    addAppInfo.setOrderNo(appList.get(0).getOrderNo());
                    addAppInfo.setStart(0);
                    /** 根据我当前登录的账号，和流程配置的级别，找到那个人、 */
                    int pid = getDataDao.getNowLevelByUserId(user.getId());
                    /** 执行递归查询 */
                    addAppInfo.setVerifier(getPid(appList.get(0).getPost(), pid));

                } else if (appList != null && appList.size() >= 2) {
                    addAppInfo.setNowNode(appList.get(0).getId());
                    addAppInfo.setOrderNo(appList.get(0).getOrderNo());
                    /** 根据我当前登录的账号，和流程配置的级别，找到那个人、 */
                    int pid = getDataDao.getNowLevelByUserId(user.getId());
                    /** 执行递归查询 */
                    addAppInfo.setVerifier(getPid(appList.get(0).getPost(), pid));
                    addAppInfo.setStart(appList.get(1).getId());
                }

                // 然后判断下一个节点和下下节点是不是最后一个节点
            } else {
                // 有数据；2·在流程的中间，那么返回它的，顺序、是否为起点、是否为终点。
                List<AuditProcessModel> appList2 =
                        getDataDao.findAppNodeInfo(nowUserNodeInfo.get(0).getOrderNo());
                /** 上一步，一定是0 ，因为我是申请啊。 */
                addAppInfo.setComp(0);
                /** 下一步，是流程的下一步 */
                addAppInfo.setStart(nowUserNodeInfo.get(0).getId());
                addAppInfo.setNowNode(appList2.get(0).getId());
                addAppInfo.setOrderNo(appList2.get(0).getOrderNo());
                int pid = getDataDao.getNowLevelByUserId(user.getId());
                /** 执行递归查询 */
                addAppInfo.setVerifier(getPid(appList2.get(0).getPost(), pid));
            }
            getDataDao.insertVerifyProcess(addAppInfo);
            logService.saveLog("核算成本清单提交", "项目号为：【" + order.getProCode() + "】的成本清单提交了");
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("提交失败，请联系管理员！");
        }
        return jsonResult;
    }

    /**
     * estimatingSubmit 概算清单
     *
     * @param id
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2021/1/7 20:27
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult estimatingSubmit(Integer id) {
        JsonResult jsonResult = new JsonResult();
        BusBudgetAccountingTaskModel order = getDataDao.getBusBudgetAccountingTaskByPrimaryKey(id);
        Date currentTime = new Date();
        User user = ShiroUtils.getUser();
        try {
            // 首先修改核算的状态
            BusBudgetAccountingTaskModel accountingTaskModel = new BusBudgetAccountingTaskModel();
            accountingTaskModel.setId(id);
            /** 0=未提交，1=已提交/提交待审，2=审核通过，3=被驳回 */
            accountingTaskModel.setSubmitStatus(1);
            accountingTaskModel.setSubmitDate(currentTime);
            getDataDao.updateBusBudgetAccountingTaskByPrimaryKeySelective(accountingTaskModel);
            // 获取单据表如果单据表不存在则插入单据表信息
            AuditProcessModel orderSheet = getDataDao.getSheetDetailModelByBllId(id, 7);
            if (orderSheet == null) {
                orderSheet = new AuditProcessModel();
                orderSheet.setBllId(id);
                orderSheet.setSubmitUser(user.getId());
                orderSheet.setSubmitTime(currentTime);
                orderSheet.setAppType(7);
                getDataDao.insertSheetDetail(orderSheet);
            }
            // 生成下一步待审核的记录************************
            // 01 首先需要查询对应的流程信息
            AuditProcessModel addAppInfo = new AuditProcessModel();
            /** 执行成功以后，获取数据的主键 */
            addAppInfo.setSheetId(orderSheet.getId());
            addAppInfo.setCode("GS001");
            /** 0=被驳回，1=未审核，2=已审核 */
            addAppInfo.setAppStatus(1);

            // 循环判断 当前的账号所在的流程节点；
            List<AuditProcessModel> nowUserNodeInfo = getDataDao.findAppNodeInfoByUserIdToEst(user.getId());
            // 分2种情况
            if (nowUserNodeInfo == null || nowUserNodeInfo.size() == 0) {
                // 无数据；1·没有再审批节点中，是员工级别的，那么直接就按照流程顺序走。
                List<AuditProcessModel> appList = getDataDao.findAppNodeInfoToEst(0);
                // 然后在找下一个节点和下下节点。
                /** 上一个节点是0 */
                addAppInfo.setComp(0);
                /** 判断流程节点，是1个还是2个及以上。 */
                if (appList != null && appList.size() == 1) {
                    // 获取第一条数据，为当前的节点号，执行人。
                    // 获取第二条数据的，节点。
                    addAppInfo.setNowNode(appList.get(0).getId());
                    addAppInfo.setOrderNo(appList.get(0).getOrderNo());
                    addAppInfo.setStart(0);
                    /** 根据我当前登录的账号，和流程配置的级别，找到那个人、 */
                    int pid = getDataDao.getNowLevelByUserIdToEst(user.getId());
                    /** 执行递归查询 */
                    addAppInfo.setVerifier(getPid(appList.get(0).getPost(), pid));

                } else if (appList != null && appList.size() >= 2) {
                    addAppInfo.setNowNode(appList.get(0).getId());
                    addAppInfo.setOrderNo(appList.get(0).getOrderNo());
                    /** 根据我当前登录的账号，和流程配置的级别，找到那个人、 */
                    int pid = getDataDao.getNowLevelByUserIdToEst(user.getId());
                    /** 执行递归查询 */
                    addAppInfo.setVerifier(getPid(appList.get(0).getPost(), pid));
                    addAppInfo.setStart(appList.get(1).getId());
                }

                // 然后判断下一个节点和下下节点是不是最后一个节点
            } else {
                // 有数据；2·在流程的中间，那么返回它的，顺序、是否为起点、是否为终点。
                List<AuditProcessModel> appList2 =
                        getDataDao.findAppNodeInfoToEst(nowUserNodeInfo.get(0).getOrderNo());
                /** 上一步，一定是0 ，因为我是申请啊。 */
                addAppInfo.setComp(0);
                /** 下一步，是流程的下一步 */
                addAppInfo.setStart(nowUserNodeInfo.get(0).getId());
                addAppInfo.setNowNode(appList2.get(0).getId());
                addAppInfo.setOrderNo(appList2.get(0).getOrderNo());
                int pid = getDataDao.getNowLevelByUserIdToEst(user.getId());
                /** 执行递归查询 */
                addAppInfo.setVerifier(getPid(appList2.get(0).getPost(), pid));
            }
            getDataDao.insertVerifyProcess(addAppInfo);
            logService.saveLog("概算成本清单提交", "项目号为：【" + order.getProCode() + "】的成本清单提交了");
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("提交失败，请联系管理员！");
        }
        return jsonResult;
    }

    /**
     * 递归：直到找到审批级别的上级人员id为止
     *
     * @param post
     * @param pid
     * @return
     */
    private int getPid(int post, int pid) {
        int userId = 0;
        AuditProcessModel nextModel = getDataDao.findNextLevelUserByPId(pid);
        if (nextModel != null) {
            if (nextModel.getPost() == post) {
                userId = nextModel.getSubmitUser();
            } else {
                getPid(post, nextModel.getParentId());
            }
        }
        return userId;
    }

    /**
     * findApprovalList 获取核算成本清单提审核表
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/25 8:47
     * @version 1.0
     */
    public List<AccountingProjectModel> findApprovalList(AccountingProjectModel param) {
        param.setCurrentUserId(ShiroUtils.getUserId());
        return getDataDao.findApprovalList(param);
    }

    /**
     * getReviewerInfo 获取当前审核流程信息
     *
     * @param batId 单据表主键
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel
     * @throws
     * @author 徐一贺
     * @date 2020/11/25 13:29
     */
    public AccountingProjectModel getReviewerInfo(Integer batId, Integer userId) {
        AccountingProjectModel condition = new AccountingProjectModel();
        condition.setBatId(batId);
        condition.setCurrentUserId(userId);
        return getDataDao.getEndReviewerInfo(condition);
    }

    /**
     * getVerifyProcessId 获取当前审核流程信息
     *
     * @param batId 单据表主键
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel
     * @throws
     * @author 徐一贺
     * @date 2020/11/25 13:29
     */
    public AccountingProjectModel getVerifyProcessId(Integer batId, Integer userId) {
        AccountingProjectModel condition = new AccountingProjectModel();
        condition.setBatId(batId);
        condition.setCurrentUserId(userId);
        return getDataDao.getReviewerInfo(condition);
    }

    /**
     * getVerifyProcessId 获取当前审核流程信息
     *
     * @param batId 单据表主键
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel
     * @throws
     * @author 徐一贺
     * @date 2020/11/25 13:29
     */
    public AccountingProjectModel getEstVerifyProcessId(Integer batId, Integer userId) {
        AccountingProjectModel condition = new AccountingProjectModel();
        condition.setBatId(batId);
        condition.setCurrentUserId(userId);
        return getDataDao.getEstReviewerInfo(condition);
    }

    /**
     * findVerifyProcessByBatId 根据子集单据表主键获取驳回列表数据
     *
     * @param batId 子集单据表主键
     * @return java.util.List<com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel>
     * @throws
     * @author 徐一贺
     * @date 2020/11/26 20:10
     */
    public List<AccountingProjectModel> findVerifyProcessByBatId(Integer batId) {
        return getDataDao.findVerifyProcessByBatId(batId);
    }

    /**
     * findSubmitListCount 获取分页数据总数
     *
     * @param param
     * @return long
     * @throws
     * @author 徐一贺
     * @date 2020/11/30 20:39
     */
    public Integer findSubmitListCount(AccountingProjectModel param) {
        param.setCurrentUserId(ShiroUtils.getUserId());
        return getDataDao.findSubmitListCount(param);
    }

    /**
     * getReviewerInfo2 获取审核人
     *
     * @param id 审核记录主键
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel
     * @throws
     * @author 徐一贺
     * @date 2020/12/1 15:20
     */
    public AccountingProjectModel getReviewerInfo2(Integer id) {
        return getDataDao.getReviewerInfo2(id);
    }

    /**
     * getReviewerInfoByBllId 根据单据表主键获取审核单据表信息
     *
     * @param batId 单据表主键
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.AccountingProjectModel
     * @author 徐一贺
     * @date 2020/12/1 16:29
     */
    public AccountingProjectModel getReviewerInfoByBllId(Integer batId) {
        return getDataDao.getReviewerInfoByBllId(batId);
    }

    /**
     * saveMainParameter 保存主要参数
     *
     * @param param 主要参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/12/10 10:25
     */
    public JsonResult saveMainParameter(BusBudgetAccountingTaskModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            getDataDao.updateBusBudgetAccountingTaskByPrimaryKeySelective(param);
            logService.saveLog("成本核算清单编辑", "保存主要参数！");
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败！");
        }
        return jsonResult;
    }

    /**
     * getNotHaveTaxTotalAmount 根据单据表主键，获取去税的总金额。总金额 = 人工 + 去税后的辅材金额。
     *
     * @param condition 单据表主键,清单项主键
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/12/14 15:27
     */
    public JsonResult getNotHaveTaxTotalAmount(BusBudgetAccountingBasicInfoConfigModel condition) {
        JsonResult jsonResult = new JsonResult();
        try {
            // 获取税
            BusBudgetAccountingCost cost =
                    getDataDao.getBusBudgetAccountingCostByPrimaryKey(condition.getCostId());
            BusBudgetAccountingTask order =
                    getDataDao.getBusBudgetAccountingTaskByPrimaryKey(cost.getBatId());
            BusBudgetAccountingEstimatesData budgetAccountingEstimatesData =
                    new BusBudgetAccountingEstimatesData();
            budgetAccountingEstimatesData.setProCode(order.getProCode());
            budgetAccountingEstimatesData.setCostId(cost.getId());
            budgetAccountingEstimatesData.setBatId(cost.getBatId());
            budgetAccountingEstimatesData.setDataMajor(cost.getMajor());
            budgetAccountingEstimatesData.setDataPart(cost.getPart());
            budgetAccountingEstimatesData.setUseType(2);
            if (cost.getCode().contains("-制造费-")) {
                if (cost.getCode().contains("-下料")
                        && "-下料".equals(cost.getCode().substring(cost.getCode().lastIndexOf("-")))) {
                    budgetAccountingEstimatesData.setDataType("该项目制造费下料不含税总价");
                } else if (cost.getCode().contains("-铆焊")
                        && "-铆焊".equals(cost.getCode().substring(cost.getCode().lastIndexOf("-")))) {
                    budgetAccountingEstimatesData.setDataType("该项目制造费铆焊不含税总价");
                } else if (cost.getCode().contains("-喷涂")
                        && "-喷涂".equals(cost.getCode().substring(cost.getCode().lastIndexOf("-")))) {
                    budgetAccountingEstimatesData.setDataType("该项目制造费喷涂不含税总价");
                } else if (cost.getCode().contains("-装配")
                        && "-装配".equals(cost.getCode().substring(cost.getCode().lastIndexOf("-")))) {
                    budgetAccountingEstimatesData.setDataType("该项目制造费装配不含税总价");
                }
            } else if (cost.getCode().contains("-现场安装费-辅材")
                    && "-辅材".equals(cost.getCode().substring(cost.getCode().lastIndexOf("-")))) {
                budgetAccountingEstimatesData.setDataType("该项目现场安装费安装人工辅材不含税总价");
            }
            // 获取工时成本
            BusBudgetAccountingEstimatesData totalTaxPrice =
                    accountingInfoDao.getBusBudgetAccountingEstimatesDataByCondition(
                            budgetAccountingEstimatesData);
            if (totalTaxPrice == null) {
                jsonResult.setData("0");
            } else if (totalTaxPrice.getDataValue() != "") {
                jsonResult.setData(totalTaxPrice.getDataValue());
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("获取不含税总价失败！");
        }
        return jsonResult;
    }

    /**
     * getBusBudgetAccountingTaskAndAppInfoByPrimaryKey 根据单据表主键获取审核者和审批者信息
     *
     * @param batId 单据表主键
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusBudgetAccountingTaskModel
     * @author 徐一贺
     * @date 2020/12/17 14:07
     */
    public BusBudgetAccountingTaskModel getBusBudgetAccountingTaskAndAppInfoByPrimaryKey(Integer batId) {
        return accountingInfoDao.getBusBudgetAccountingTaskAndAppInfoByPrimaryKey(batId);
    }

    /**
     * changeOrder 拖动改变肾虚
     *
     * @param dragItemId
     * @param prevItemId
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2021/1/7 15:37
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult changeOrder(Integer dragItemId, Integer prevItemId) {
        JsonResult jsonResult = new JsonResult();
        try {
            BusBudgetAccountingCost dragItem = accountingInfoDao.getBusBudgetAccountingCostByPrimaryKey(dragItemId);
            BusBudgetAccountingCost prevItem = accountingInfoDao.getBusBudgetAccountingCostByPrimaryKey(prevItemId);
            if (prevItem.getParentCode().equals(dragItem.getParentCode())) {
                //获取所有成本项按照序号排序
                List<BusBudgetAccountingCost> costItems = accountingInfoDao.findBusBudgetAccountingCostByBatId(dragItem.getBatId());
                boolean isChangeOrderNo = false;
                for (int i = 0; i < costItems.size(); i++) {
                    BusBudgetAccountingCost costItem = costItems.get(i);
                    if (costItem.getId().intValue() == prevItem.getId().intValue()) {
                        isChangeOrderNo = true;
                        dragItem.setOrderNo(prevItem.getOrderNo() + 1);
                        accountingInfoDao.updateBusBudgetAccountingCostByPrimaryKey(dragItem);
                    }
                    if (isChangeOrderNo && costItem.getId().intValue() != prevItem.getId().intValue()) {
                        costItem.setOrderNo(costItem.getOrderNo() + 1);
                        accountingInfoDao.updateBusBudgetAccountingCostByPrimaryKey(costItem);
                    }
                }
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setMsg("排序成功！");
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
                jsonResult.setMsg("此项只能在自己父级项内改变顺序！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("排序失败！");
        }
        return jsonResult;
    }

    /**
     * @methodName: exportCostSheet
     * @description: 导出成本清单
     * @param: response
     * @param: param
     * @return： void
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 14:49
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    public void exportCostSheet(HttpServletResponse response, BusBudgetAccountingTaskModel param) {
        List<CostSheet> list = new ArrayList<>();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        Subject userSubject = ShiroUtils.getSubject();
        // 1.创建临时内存表名
        String tempTableName = "##" + ShiroUtils.getUser().getAccount() + (UUID.randomUUID().toString().replace("-", ""));
        param.setTempTableName(tempTableName);
        // 2.查询出数据导入到临时表
        accountingInfoDao.createTempTable(param);
        // 3.查询临时内存表数据导出
        Integer dataCount = accountingInfoDao.getTempTableDataCount(param);
        int step = 5000;
        FileUtils.mkdirs(path);
        // 方法1 如果写到同一个sheet
        path = path + "生产进度看板查询列表.xlsx";
        ExcelWriter excelWriter = null;
        try {
            // 这里 需要指定写用哪个class去写
            excelWriter = EasyExcel.write(path, CostSheet.class).build();
            // 这里注意 如果同一个sheet只要创建一次
            WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
            int rank1Num=0;
            int rank2Num=0;
            int rank3Num=0;
            int rank4Num=0;
            int rank5Num=0;
            for (int topCount = 5000, notHaveTopCount = 0;
                 notHaveTopCount < dataCount;
                 notHaveTopCount += step) {
                param.setTopCount(topCount);
                param.setNotHaveTopCount(notHaveTopCount);
                list = accountingInfoDao.selectTempTableByParam(param);
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getRank().intValue() == 1) {
                        rank1Num++;
                        list.get(i).setOrderNo(getChineseNo(rank1Num));
                        rank2Num=0;
                        rank3Num=0;
                    }else if (list.get(i).getRank().intValue() == 2) {
                        rank2Num++;
                        list.get(i).setOrderNo("("+getChineseNo(rank2Num)+")");
                        rank3Num=0;
                    }else if (list.get(i).getRank().intValue() == 3) {
                        rank3Num++;
                        list.get(i).setOrderNo(rank3Num+"");
                        rank4Num=0;
                    }else if (list.get(i).getRank().intValue() == 4) {
                        rank4Num++;
                        list.get(i).setOrderNo(rank3Num+"."+rank4Num);
                        rank5Num=0;
                    }else if (list.get(i).getRank().intValue() == 5) {
                        rank5Num++;
                        list.get(i).setOrderNo(rank3Num+"."+rank4Num+"."+rank5Num);
                    }
                }
                if (list != null) {
                    excelWriter.write(list, writeSheet);
                }
            }
            // 4.删除临时内存表
            accountingInfoDao.dropTempTable(tempTableName);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        // 5.下载
        FileUtils.downloadFile(path, response);
    }

    private String[] changeNum = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private String[] unit = new String[]{"", "十", "百", "千", "万"};
    /**
     * @methodName: getChineseNo
     * @description: 获取中文序号
     * @param: num
     * @return： java.lang.String
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 18:09
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    private String getChineseNo(Integer num) {
        String overWan = Math.floor(num / 10000) + "";
        String noWan = (num % 10000) + "";
        if (noWan.toString().length() < 4) {
            noWan = noWan.toString();
        }
        return Double.valueOf(overWan)>10000 ? getWan(overWan) + "万" + getWan(noWan) : getWan(num.toString());
    }

    /**
     * @methodName: reverse
     * @description: 翻转数组
     * @param: arr
     * @return： java.lang.String[]
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 16:59
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    public int[] reverse(String[] arr) {
        int[] res = new int[arr.length];
        for (int i = arr.length-1, j = 0; i >= 0; i--, j++) {
            res[j] = Integer.parseInt(arr[i]);
        }
        return res;
    }
    /**
     * @methodName: getWan
     * @description: 获取数
     * @param: num
     * @return： java.lang.String
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 18:06
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    public String getWan(String num) {
        if(new BigDecimal(num).compareTo(BigDecimal.ZERO)==0){
            num="0";
        }
        int[] strArr = reverse(num.toString().split(""));
        String newNum = "";
        for (int i = 0; i < strArr.length; i++) {
            newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
        }
        return newNum;
    }
}
